package com.djinquan.common.interceptor;

import com.djinquan.common.util.FunUtil;
import com.djinquan.common.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Desc:
 * @Group:美的集团-营销系统-技术组
 * @Author dengjq15
 * @Date 2024/3/30
 */
@Component
public class LoginCheckInterceptor implements HandlerInterceptor {

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //不存在则退出
        String loginName = (String) redisUtil.getObject("token");
        if(FunUtil.objIsNull(loginName)){
            request.getRequestDispatcher("/loginOut").forward(request,response);
            return false;
        }
        //存在则续期1分钟
        redisUtil.setExprice("token",1L);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
